package com._51cto.healthy.controller;

import com._51cto.healthy.content.P;
import com._51cto.healthy.content.R;
import com._51cto.healthy.pojo.Order;
import com._51cto.healthy.service.OrderService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

@RestController
@RequestMapping("/order")
public class OrderController {

    @DubboReference
    private OrderService orderService;
    @Autowired
    private HttpSession session;

    @GetMapping("/page/{pageSize}/{pageNum}")
    public R queryList(@RequestParam(value = "query",required = false/*表示参数不是必填项*/) String query,
                       @PathVariable("pageNum") Integer pageNum,
                       @PathVariable("pageSize") Integer pageSize){
        P<Order> orderP = orderService.queryList(query, pageNum, pageSize);
        return R.success(orderP);

    }

    @PostMapping()
    public R insert(@RequestBody Order order){
        if(order.getId()!=null){
            orderService.update(order);
            return R.success("修改成功");
        }else {
            Integer userId = (Integer) session.getAttribute("userId");
            //时间加一天
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(order.getOrderDate());
            calendar.add(Calendar.DATE,1);
            Date time = calendar.getTime();
            order.setOrderDate(time);
            Date date = new Date();

            if ((order.getOrderDate().getTime() - date.getTime()) > 0) {
                orderService.insertAll(order,userId);
            }else {
                throw new IllegalArgumentException("预约日期已过，请重新预约！");
            }
            return R.success("预约成功");
        }

    }

//    @PostMapping()
//    public R update(@RequestBody Order order){
//        orderService.update(order);
//        return R.success("更新成功");
//    }



}
